﻿// Author: Alexander Jhin

package com.societyGames.system
{
import flash.events.Event;
import flash.events.IEventDispatcher;

/**
 * ...
 * @author Alexander Jhin
 */
public class EventObject
{
  private var _eventDispatcher:IEventDispatcher; //The event dispatcher to raise of the event on.
  private var _type:String;	//The string type of the event.
  private var _eventClass:Class;

  public function EventObject(eventDispatcher:IEventDispatcher, type:String, eventClass:Class = null)
  {
    this._eventDispatcher = eventDispatcher;
    this._type = type;
    this._eventClass = eventClass;
    if (_eventClass == null)
    {
      _eventClass = Event;
    }
  }

  /// Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
  public function addEventListener(listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
  {
    this._eventDispatcher.addEventListener(this._type, listener, useCapture, priority, useWeakReference);
  }

  /// Dispatches an event into the event flow.
  public function dispatchEvent(event:Event = null):Boolean
  {
    if (event == null)
    {
      return this._eventDispatcher.dispatchEvent(new _eventClass(_type));
    }
    else
    {
      return this._eventDispatcher.dispatchEvent(event);
    }
  }

  /// Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
  public function hasEventListener():Boolean
  {
    return this._eventDispatcher.hasEventListener(this._type);
  }

  /// Removes a listener from the EventDispatcher object.
  public function removeEventListener(listener:Function, useCapture:Boolean = false):void
  {
    this._eventDispatcher.removeEventListener(this._type, listener, useCapture);
  }

  /// Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
  public function willTrigger(type:String):Boolean
  {
    return this._eventDispatcher.willTrigger(this._type);
  }

  public function get type():String
  {
    return _type;
  }
}
}